/*
 * math_util.cpp
 *
 *  Created on: Dec 13, 2012
 *      Author: filipe mutz
 */
#include "math_util.h"
#include <cmath>
#include <cstdlib>


GaussianDistribution::GaussianDistribution(Point center, Point deviation)
{
	_center = center;
	_deviation = deviation;
}


GaussianDistribution::~GaussianDistribution()
{
	_center.clear();
	_deviation.clear();
}


vector<Point>
GaussianDistribution::generate(int num_points)
{
	vector<Point> distribution;

	for(int i = 0; i < num_points; i++)
	{
		Point p;

		for(unsigned int j = 0; j < _center.size(); j++)
			p.push_back(_center[j] + random_gaussian_radius(_deviation[j]));

		distribution.push_back(p);
	}

	return distribution;
}


double
random_gaussian_radius(double std)
{
	double norm = 1.0 / (RAND_MAX + 1.0);

	double u = 1.0 - rand() * norm; /* can't let u == 0 */
	double v = rand() * norm;
	double z = sqrt(-2.0 * log(u)) * cos(2.0 * M_PI * v);

	return (std * z);
}


double
euclidean_distance(double *p, double *q, int size)
{
	double dist = 0;

	for(int i = 0; i < size; i++)
		dist += pow(p[i] - q[i], 2);

	return sqrt(dist);
}
